﻿using lxwmvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace velocity_weixin
{
    public class StringCore
    {
        /// <summary>
        /// 把Unicode转换成功中文
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Unicode(string str)  
        {  
            string outStr = "";
            if (!string.IsNullOrEmpty(str))
            {
                string[] strlist = str.Replace("//", "").Split('u');
                try
                {
                    for (int i = 1; i < strlist.Length; i++)
                    {
                        //将unicode字符转为10进制整数，然后转为char中文字符  
                        outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
                    }
                }
                catch (FormatException ex)
                {
                    outStr = ex.Message;
                }
            }
            return outStr;  
        }

        /// <summary>
        /// 返回类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objValue"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T Get<T>( Dictionary<string, object> objValue, string key)
        {
            return objValue != null && objValue.ContainsKey(key) ? objValue[key].toT<T>() : default(T);
        }

        /// <summary>
        /// 截取两个字符串之间的数据
        /// </summary>
        /// <param name="objValue"></param>
        /// <param name="indexStr"></param>
        /// <param name="lastStr"></param>
        /// <param name="iDefault"></param>
        /// <param name="throwE"></param>
        /// <returns></returns>
        public static string SubString(string objValue, string indexStr = "", string lastStr = "", string iDefault = "", bool throwE = false)
        {
            try
            {
                int index = objValue.IndexOf(indexStr);
                if (lastStr != "" && index > -1)
                {
                    objValue = objValue.Remove(0, index);
                    index = objValue.IndexOf(indexStr);
                }
                int last = objValue.IndexOf(lastStr);
                last = last == 0 ? objValue.Length : last;
                if (index > -1 && last > -1)
                {
                    objValue = objValue.Substring(index + indexStr.Length, last - (index + indexStr.Length));
                    return objValue;
                }
                else
                {
                    return iDefault;
                }

            }
            catch (Exception error)
            {
                if (throwE) throw error;
                return iDefault;
            }
        }

        /// <summary>
        /// 返回内容
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ispost"></param>
        /// <param name="bianma"></param>
        /// <returns></returns>
        public static string GetUrl(string url, bool ispost = false, string bianma = "utf-8")
        {
            try
            {
                Uri ourUri = new Uri(url);
                HttpWebRequest web = (HttpWebRequest)WebRequest.Create(ourUri);
                web.ReadWriteTimeout = 100;
                if (ispost)
                {
                    web.Method = "Post";
                }

                WebResponse myWebResponse = (HttpWebResponse)web.GetResponse();

                Encoding en = Encoding.GetEncoding(bianma);
                StreamReader stream = new StreamReader(myWebResponse.GetResponseStream(), en);

                return stream.ReadToEnd();
            }
            catch (Exception ex)
            {
                dataloger.fatal(ex.Message, ex);
                return "";
            }

        }
    }
}
